//
//  ClassListController.m
//  SleepingElf
//
//  Created by karong on 2023/10/19.
//

#import "ClassListController.h"
#import "ClassListRequest.h"
#import "HomeModel.h"
#import "ClassListCell.h"
#import "ClassMusicCell.h"
#import "UIButton+ImageAlign.h"

@interface ClassListController ()
@property (weak, nonatomic) IBOutlet UICollectionView *classListView;
@property (weak, nonatomic) IBOutlet UICollectionView *listView;
@property (weak, nonatomic) IBOutlet UIView *mainView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *mainViewBottom;
@property (weak, nonatomic) IBOutlet UIButton *playStatusBtn;
@property (nonatomic, strong) ClassListRequest * request;
@property (nonatomic, strong) HomeClassify * classify;
@property (nonatomic, assign) NSInteger classListViewSelectIndex;
@property (nonatomic, assign) NSInteger listViewSelectIndex;
@end

@implementation ClassListController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self layoutPlaystatus];
    [self loadClassListRequest];
    ClassListRequest * request = [ClassListRequest new];
    self.request = request;
    self.listViewSelectIndex = -1;
    self.view.backgroundColor = UIColor.clearColor;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^(void){
        self.mainViewBottom.constant = -40;
        [self.view uploadLayout];
    });
}

- (void)loadClassListRequest {
    MusicModel * music = [[PlayerManager shared]currentPlayingMusic];
    [ClassListRequest loadSleepPlayClasslistWithClassify_id:music.category_id success:^(id obj) {
        self.classify = obj;
        [self layoutMainView];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
    }];
}

- (IBAction)closeAction:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)playStatusAction:(id)sender {
    appConfig.playStatus ++;
    [self layoutPlaystatus];
}

- (void)layoutMainView {
    if (self.classify.children.count > 0) {
        HomeClassify * classify = self.classify.children.firstObject;
        self.request.classify_id = classify.id;
    }
    else {
        self.request.classify_id = self.classify.id;
        [self.classListView removeFromSuperview];
        [self.mainView uploadLayout];
    }
    self.titleLabel.text = self.classify.name;
    [self.classListView reloadData];
    [self beginRefresh];
}

- (void)layoutPlaystatus {
    NSInteger playStatus = appConfig.playStatus % 4;
    [self.playStatusBtn setTitle:playStatusArray[playStatus] forState:(UIControlStateNormal)];
    [self.playStatusBtn setImage:SEIMG(SESF(@"icon_playstatus_%ld", playStatus)) forState:(UIControlStateNormal)];
    [self.playStatusBtn setButtonImageTitleStyle:(GRButtonImageTitleStyleLeft) padding:10];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (collectionView == self.classListView) {
        return self.classify.children.count;
    }
    else {
        return self.dataArray.count;
    }
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell * cell;
    if (collectionView == self.classListView) {
        ClassListCell * classListCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ClassListCell" forIndexPath:indexPath];
        [classListCell setDataWithObject:self.classify.children[indexPath.row] select:self.classListViewSelectIndex == indexPath.row];
        cell = classListCell;
    }
    else {
        ClassMusicCell * classMusicCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ClassMusicCell" forIndexPath:indexPath];
        [classMusicCell setDataWithObject:self.dataArray[indexPath.row] select:self.listViewSelectIndex == indexPath.row];
        cell = classMusicCell;
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.classListView) {
        self.classListViewSelectIndex = indexPath.row;
        self.listViewSelectIndex = -1;
        HomeClassify * classify = self.classify.children[indexPath.row];
        self.request.classify_id = classify.id;
        [self beginRefresh];
    }
    else {
        self.listViewSelectIndex = indexPath.row;
        [PlayerManager shared].musicList = self.dataArray;
        if ([[PlayerManager shared]musicIsPlaying:self.dataArray[indexPath.row]]) {
            [[PlayerManager shared]pause];
        }
        else {
            [[PlayerManager shared]playListWithMusic:self.dataArray[indexPath.row]];
        }

    }
    [collectionView reloadData];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = CGSizeZero;
    if (collectionView == self.classListView) {
        HomeClassify * homeClassify = self.classify.children[indexPath.row];
        CGFloat width = [homeClassify.name boundingRectWithSize:CGSizeMake(MAXFLOAT, 0.0) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14 weight:(UIFontWeightRegular)]} context:nil].size.width;
        size.width = width + 26;
        size.height = 26;
    }
    else {
        size.width = SEScreenWidth;
        size.height = 54;
    }
    return size;
}

@end
